package com.situ.supermarket.service.impl;

import com.situ.supermarket.dao.AreaDao;
import com.situ.supermarket.dao.GoodsDao;
import com.situ.supermarket.entity.Area;
import com.situ.supermarket.entity.Goods;
import com.situ.supermarket.service.AreaService;
import com.situ.supermarket.vo.ResultVO;
import com.situ.supermarket.vo.TableVO;

import java.time.LocalDateTime;
import java.util.List;

public class AreaServiceImpl implements AreaService {
    AreaDao dao = new AreaDao();


    @Override
    public List<Area> all() {
        return dao.all();
    }

    @Override
    public ResultVO delete(Integer id) {
        //判断分区有没有被用过
        //检测
        GoodsDao gdao = new GoodsDao();
        List<Goods> list = gdao.selectId(id);
        ResultVO vo = new ResultVO();
        if(!list.isEmpty()){
            vo.setCode(2);
            vo.setMsg("被使用中，无法删除");
        }else {
            dao.delete(id);
        }
        return vo;
    }

    @Override
    public TableVO select(Integer page, Integer limit, String name) {
        List<Area> list=dao.select(page,limit,name);
        TableVO vo=new TableVO();
        vo.setCount(dao.count(name));
        vo.setData(list);
        return vo;
    }

    @Override
    public void save(Area a) {
        if(a.getId()==null){
            dao.insert(a);
        }else {
            dao.update(a);
        }
    }

    @Override
    public Area info(Integer id) {
        List<Area> list= dao.selectById(id);
        if(list.isEmpty()){
            return null;
        }else {
            return list.get(0);
        }
    }
}
